{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Verify AirFlow is ready to Start DAG"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import boto3\n",
    "import requests\n",
    "\n",
    "session = boto3.session.Session()\n",
    "region = session.region_name\n",
    "account_id = boto3.client(\"sts\").get_caller_identity().get(\"Account\")\n",
    "\n",
    "dag_name = \"bert_reviews\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%store -r airflow_bucket_name\n",
    "%store -r airflow_env_name\n",
    "%store -r team_role_arn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mwaa = boto3.client(\"mwaa\")\n",
    "mwaa_status = \"\"\n",
    "\n",
    "\n",
    "def get_airflow_check():\n",
    "    response = mwaa.get_environment(Name=airflow_env_name)\n",
    "    mwaa_status = response[\"Environment\"][\"Status\"]\n",
    "    return mwaa_status\n",
    "\n",
    "\n",
    "mwaa_status = get_airflow_check()\n",
    "if mwaa_status != \"AVAILABLE\":\n",
    "    print(\"[ERROR] Cannot find MWAA {}.\".format(airflow_env_name))\n",
    "else:\n",
    "    print(\"Sucess! {} is ready!\".format(airflow_env_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PLEASE MAKE SURE THAT THE ABOVE COMMAND RAN SUCESSFULLY BEFORE CONTINUING"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Triggering DAG from MWAA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "mwaa_cli_token = mwaa.create_cli_token(Name=airflow_env_name)\n",
    "\n",
    "cli_token = \"Bearer \" + mwaa_cli_token[\"CliToken\"]\n",
    "mwaa_web_server_hostname = \"https://\" + mwaa_cli_token[\"WebServerHostname\"] + \"/aws_mwaa/cli\"\n",
    "\n",
    "raw_data = \"trigger_dag {}\".format(dag_name)\n",
    "\n",
    "response = requests.post(\n",
    "    mwaa_web_server_hostname, headers={\"Authorization\": cli_token, \"Content-Type\": \"text/plain\"}, data=raw_data\n",
    ")\n",
    "\n",
    "if response.status_code != 200:\n",
    "    print(\"ERROR: DAG: {} failed to get triggered!\".format(dag_name))\n",
    "else:\n",
    "    print(\"Sucess! DAG: {} was triggered successfuly\".format(dag_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Release Resources"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%html\n",
    "\n",
    "<p><b>Shutting down your kernel for this notebook to release resources.</b></p>\n",
    "<button class=\"sm-command-button\" data-commandlinker-command=\"kernelmenu:shutdown\" style=\"display:none;\">Shutdown Kernel</button>\n",
    "        \n",
    "<script>\n",
    "try {\n",
    "    els = document.getElementsByClassName(\"sm-command-button\");\n",
    "    els[0].click();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}    \n",
    "</script>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%javascript\n",
    "\n",
    "try {\n",
    "    Jupyter.notebook.save_checkpoint();\n",
    "    Jupyter.notebook.session.delete();\n",
    "}\n",
    "catch(err) {\n",
    "    // NoOp\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
